package cn.rengy.web.framework.id;


import java.util.Date;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.rengy.tool.core._Snowflake;
import cn.rengy.tool.core.util._DateUtils;
import cn.rengy.web.framework.sys.worker.SysWorkerNodeService;

/**
 * 
 * 雪花算法生成id，单机实现
 *  自定义实现可参考https://github.com/baidu/uid-generator/blob/master/README.zh_cn.md
 * @author rengy
 * 
 *
 */
@Component
public class DefaultIdGenerator implements IdGenerator{

    //_DateUtils.parseDate("2019-12-10", "yyyy-MM-dd"),
    private _Snowflake snowflake;
    @Autowired
    private SysWorkerNodeService sysWorkerNodeService;
    
    
    @PostConstruct
	public void init(){
    	snowflake=new _Snowflake(sysWorkerNodeService.getWorkerId(),true);
    }
    

	@Override
	public Long generateId(){
		return snowflake.nextId();
	}
	@Override
	public Long nextId(){
		return snowflake.nextId();
	}
	@Override
	public Long createId(Date time){
		return snowflake.createId(time.getTime());
	}
	@Override
	public String parseId(Long id){
		return _DateUtils.formatDate(snowflake.getGenerateDateTime(id), null, "yyyy-MM-dd HH:mm:ss.SSS");
	}
	@Override
	public String nowDate() {
		return snowflake.getSnowflakeClockTime();
	}
	
}
